package xxy.com.customview;

import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import xxy.com.customview.adapter.Main5Adapter;
import xxy.com.customview.divider.RecycleViewDivider;
import xxy.com.customview.util.UIUtils;
import xxy.com.customview.view.MyScrollView;

public class Main5Activity extends AppCompatActivity {

    @BindView(R.id.main5_rl)
    RecyclerView rl;

    @BindView(R.id.main5_view)
    View view_red;
    @BindView(R.id.main5_view_top)
    ImageView iv;
    @BindView(R.id.main5_msv)
    MyScrollView mScrollView;
    @BindView(R.id.main5_bt)
    Button main5Bt;
    private int iv_height;
    private boolean isIntercept;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main5);
        ButterKnife.bind(this);
        init();
        initView();
        setClickListener();
    }

    public void init() {

    }

    private void initView() {
        //设置recyclerview
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rl.setLayoutManager(linearLayoutManager);
        Main5Adapter main5Adapter = new Main5Adapter(this);
        //分割线
        RecycleViewDivider recycleViewDivider = new RecycleViewDivider(this, LinearLayoutManager.HORIZONTAL, UIUtils.dp2Px(1, this), getResources().getColor(R.color.pay_reserve_list_divider_white));
        rl.addItemDecoration(recycleViewDivider);
//        rl.setAnimation(AnimationUtils.loadAnimation(this,R.anim.rv_itemview_in));
//        rl.setNestedScrollingEnabled(true);
        rl.setAdapter(main5Adapter);
    }

    /**
     * 设置监听
     */
    private void setClickListener() {
        //调用Myscrollview里面的定义好的接口
        mScrollView.setInterceptHeight(new MyScrollView.InterceptHeight() {
            @Override
            public boolean mInterceptHeight(int x, int y, int oldx, int oldy) {
                Log.e("height", "scrollY:\t" + y);
                if (y < iv_height) {
                    isIntercept = true;
                    return true;
                }
                isIntercept = false;
                return false;
            }
        });

        mScrollView.setFoucus(new MyScrollView.RequestFocus() {
            @Override
            public void requestFocus() {
                rl.requestFocus();
//                rl.setFocusable(true);
            }
        });

//        rl.setIntercept(new MyRecyclerView.Intercept() {
//            @Override
//            public void mInterceptMotionEvent(MotionEvent motionEvent) {
//                switch (motionEvent.getAction()) {
//                    case MotionEvent.ACTION_DOWN:
//                        if (isIntercept) {
//                            mScrollView.requestDisallowInterceptTouchEvent(false);
//                        } else {
//                            mScrollView.requestDisallowInterceptTouchEvent(true);
//                        }
//                        break;
//                    case MotionEvent.ACTION_MOVE:
//                        if (isIntercept) {
//                            mScrollView.requestDisallowInterceptTouchEvent(false);
//                        } else {
//                            mScrollView.requestDisallowInterceptTouchEvent(true);
//                        }
//                        break;
//                    case MotionEvent.ACTION_UP:
//                        break;
//                }
//            }
//        });
    }

    /**
     * activity里的UI都加载好后会调用的方法
     *
     * @param hasFocus
     */
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        iv_height = iv.getHeight();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int heightPixels = displayMetrics.heightPixels;
        Log.e("height", "iv_height:\t" + iv_height + "\t\tgetHeight:\t" + heightPixels);
    }

    @OnClick(R.id.main5_bt)
    public void onViewClicked() {
        startActivity(new Intent(Main5Activity.this, Main6Activity.class), ActivityOptions.makeSceneTransitionAnimation(Main5Activity.this).toBundle());
    }
}
